//
//  HWViewExtension.swift
//  WoWoLiveAssistant
//
//  Created by 王昌健 on 2021/10/28.
//

import UIKit

extension UIView {
    func addCorner(conrners: UIRectCorner , radius: CGFloat) {
        
        let maskPath = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: conrners, cornerRadii: CGSize(width: radius, height: radius))
        let maskLayer = CAShapeLayer()
        maskLayer.frame = self.bounds
        //        maskLayer.lineWidth = 1
        //        maskLayer.borderColor = UIColor.red.cgColor
        //        maskLayer.strokeColor = UIColor.red.cgColor
        maskLayer.path = maskPath.cgPath
        self.layer.mask = maskLayer
        
        //        self.layer.mask?.masksToBounds = true
        //        self.layer.masksToBounds = true
    }
    
    func addCornerAndBorder(conrners: UIRectCorner , radius: CGFloat, borderWidth:CGFloat,borderColor:UIColor) {
        
        let maskLayer = CAShapeLayer()
        maskLayer.frame = self.bounds
        maskLayer.path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: conrners, cornerRadii: CGSize(width: radius, height: radius)).cgPath
        self.layer.mask = maskLayer
        //        UIBezierPath(roundedRect: bgView.bounds,height: 25)).CGPath
        // Add border
        let borderLayer = CAShapeLayer()
        borderLayer.path = maskLayer.path // Reuse the Bezier path
        borderLayer.fillColor = UIColor.clear.cgColor
        borderLayer.strokeColor = borderColor.cgColor
        borderLayer.lineWidth = borderWidth
        borderLayer.frame = self.bounds
        self.layer.addSublayer(borderLayer)
    }
    
    func addCornerAndShadow(conrners: UIRectCorner , radius: CGFloat, shadowColor:UIColor,shadowOffset:CGSize,shadowRadius:CGFloat,shadowOpacity:Float) {
        
        //        self.layer.shadowColor = UIColor.gray.cgColor
        //        self.layer.shadowOffset = CGSize.init(width: 0, height: 0)
        //        self.layer.shadowOpacity = 0.8//阴影透明度
        //        self.layer.shadowRadius = 8//阴影半径
        //                //切圆角
        //        self.layer.cornerRadius = 50
        //                //路径阴影
        //                let path = UIBezierPath.init(roundedRect: self.bounds, byRoundingCorners: .topLeft, cornerRadii: CGSize.init(width: 50, height: 50))
        //                //设置阴影路径
        //        self.layer.shadowPath = path.cgPath
        
        //        let maskLayer = CAShapeLayer()
        //        maskLayer.frame = self.bounds
        //        maskLayer.path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: conrners, cornerRadii: CGSize(width: radius, height: radius)).cgPath
        //        self.layer.mask = maskLayer
        ////        UIBezierPath(roundedRect: bgView.bounds,height: 25)).CGPath
        //        // Add border
        //        let borderLayer = CAShapeLayer()
        //        borderLayer.path = maskLayer.path // Reuse the Bezier path
        //        borderLayer.fillColor = UIColor.red.cgColor
        //        borderLayer.shadowColor = shadowColor.cgColor
        //        borderLayer.shadowOffset = shadowOffset
        //        borderLayer.shadowRadius = shadowRadius
        //        borderLayer.shadowOpacity = shadowOpacity
        //        borderLayer.frame = self.bounds
        //        self.layer.addSublayer(borderLayer)
        
        
        
        //        let maskLayer = CAShapeLayer()
        //        maskLayer.frame = self.bounds
        //        maskLayer.path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: conrners, cornerRadii: CGSize(width: radius, height: radius)).cgPath
        //        self.layer.mask = maskLayer
        //        UIBezierPath(roundedRect: bgView.bounds,height: 25)).CGPath
        // Add border
        //        let borderLayer = CAShapeLayer()
        //        borderLayer.path = maskLayer.path // Reuse the Bezier path
        //        borderLayer.fillColor = UIColor.clear.cgColor
        //        borderLayer.strokeColor = borderColor.cgColor
        //        borderLayer.lineWidth = borderWidth
        //        borderLayer.frame = self.bounds
        //        self.layer.addSublayer(borderLayer)
    }
}

extension String {
    //Range转换为NSRange
    func toNSRange(_ range: Range<String.Index>) -> NSRange {
        guard let from = range.lowerBound.samePosition(in: utf16), let to = range.upperBound.samePosition(in: utf16) else {
            return NSMakeRange(0, 0)
        }
        return NSMakeRange(utf16.distance(from: utf16.startIndex, to: from), utf16.distance(from: from, to: to))
    }
    
    //NSRange转换为Range
    func toRange(_ range: NSRange) -> Range<String.Index>? {
        guard let from16 = utf16.index(utf16.startIndex, offsetBy: range.location, limitedBy: utf16.endIndex) else { return nil }
        guard let to16 = utf16.index(from16, offsetBy: range.length, limitedBy: utf16.endIndex) else { return nil }
        guard let from = String.Index(from16, within: self) else { return nil }
        guard let to = String.Index(to16, within: self) else { return nil }
        return from ..< to
    }
    
}
